package com.mall.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.entity.Category;
import com.mall.mapper.CategoryMapper;
import com.mall.service.ICategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *  分类服务实现类
 * @author sppxpp
 * @since 2023-04-14
 */
@Service
public class CategoryServiceImpl implements ICategoryService {
    private static final String PARENT_ID = "parent_id";
    private static final String IS_DELETE = "is_delete";

    final
    CategoryMapper categoryMapper;

    public CategoryServiceImpl(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    /**
     * 查询分类
     * @param page  page
     * @return records
     */
    @Override
    public IPage<Category> queryCategories(Page<Category> page) {

        return categoryMapper.selectPage(page,new QueryWrapper<Category>().eq(PARENT_ID,"0").eq(IS_DELETE,false));
    }

    @Override
    public IPage<Category> queryCategories(Page<Category> page, String parentId) {
        return categoryMapper.selectPage(page,new QueryWrapper<Category>().eq(PARENT_ID,parentId).eq(IS_DELETE,false));
    }

    /**
     * 增加第一级分类
     * @param category 分类实体
     * @return bool
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addCategoryLevelOne(Category category) {
        category.setParentId("0");
        category.setCategoryLevel(1);
        if (categoryMapper.insert(category) > 0) {
            //如：家电 数码 手机，自动添加家电数码手机三个二级分类
            String[] children = category.getCategoryName().split(" ");
            for (String child : children) {
                Category category2 = new Category(null, 2, category.getCategoryId(), child, null, false,null);
                if (!addCategoryLevelTwoOrThree(category2)) {
                    throw new RuntimeException("添加分类失败");
                }
            }
            return true;
        }
        return false;
    }

    /**
     * 增加第二级分类
     * @param category 分类实体
     * @return bool
     */
    @Override
    public Boolean addCategoryLevelTwoOrThree(Category category) {
        return categoryMapper.insert(category) > 0;
    }

    /**
     * 修改分类
     *
     * @param category entity
     * @return bool
     */
    @Override
    public Boolean updateCategory(Category category) {
        return categoryMapper.updateById(category) > 0;
    }

    /**
     * 删除分类
     * @param ids ids
     * @return bool
     */
    @Override
    public int deleteCategory(List<String> ids) {
        AtomicInteger count = new AtomicInteger();
        ids.forEach(item->{
            Category category = new Category();
            category.setCategoryId(item);
            category.setIsDelete(true);
            categoryMapper.updateById(category);
            count.getAndIncrement();
        });
        return count.get();
    }
}
